/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.model.storing;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jdom.Attribute;
import org.jdom.Element;
import ru.ssau.entities.User;
import ru.ssau.entities.Message;
import ru.ssau.lib.StringIntegerPair;
import ru.ssau.lib.XMLizable;
import ru.ssau.utils.XMLHelper;

/**
 *
 * @author Дима
 */
public class ForumMapDAO implements ForumDataAccessObject, XMLizable {
    
    private Map<Integer, User> users = new HashMap<Integer, User>();
    private Map<Integer, Message> messages = new HashMap<Integer, Message>();
    private Map<Integer, ru.ssau.entities.Thread> threads = new HashMap<Integer, ru.ssau.entities.Thread>();
    
    private int lastUserId = 1;
    private int lastThreadId = 1;
    private int lastMessageId = 1;
    
    
    public void addThread(ru.ssau.entities.Thread thread) {
        thread.setId(lastThreadId);
        lastThreadId++;
        threads.put(Integer.valueOf(thread.getId()), thread);
    }

    public void replaceThread(ru.ssau.entities.Thread thread) {
        threads.put(Integer.valueOf(thread.getId()), thread);
    }

    public ru.ssau.entities.Thread getThreadById(Integer id) {
        return threads.get(id);
    }

    public void deleteThreadById(Integer id) {
        if (threads.isEmpty()) {
            return;
        }

        ru.ssau.entities.Thread t = threads.remove(id);
        if (t == null) {
            return;
        }
        int deletingId = t.getId();

        Collection<Message> messageCollection = messages.values();
        Iterator<Message> iterator = messageCollection.iterator();
        while (iterator.hasNext()) {
            Message m = iterator.next();
            if (m.getThreadId().equals(deletingId)) {
                iterator.remove();
            }
        }
    }

    public Collection<ru.ssau.entities.Thread> getThreads() {
        return threads.values();
    }

    public void setThreads(Map<Integer, ru.ssau.entities.Thread> threads) {
        this.threads = threads;
    }

    public void addMessage(Message message) {
        message.setId(lastMessageId);
        lastMessageId++;
        messages.put(Integer.valueOf(message.getId()), message);
    }
    
    public void replaceMessage(Message message) {
        messages.put(Integer.valueOf(message.getId()), message);
    }

    public Message getMessageById(Integer id) {
        return messages.get(id);
    }

    public void deleteMessageById(Integer id) {
        if (messages.isEmpty()) {
            return;
        }

        Message m = messages.remove(id);
        if (m == null) {
            return;
        }
    }

    public Collection<Message> getMessages() {
        return messages.values();
    }

    public void setMessages(Map<Integer, Message> messages) {
        this.messages = messages;
    }

    public void addUser(User user) {
        user.setId(lastUserId);
        lastUserId++;
        users.put(Integer.valueOf(user.getId()), user);
    }

    public void replaceUser(User user) {
        users.put(Integer.valueOf(user.getId()), user);
    }
    
    public User getUserById(Integer id) {
        return users.get(id);
    }

    public void deleteUserById(Integer id) {
        if (users.isEmpty()) {
            return;
        }

        User u = users.remove(id);
        if (u == null) {
            return;
        }

        Integer deletingId = u.getId();
        Collection<Message> messageCollection = messages.values();
        Iterator<Message> iterator = messageCollection.iterator();
        while (iterator.hasNext()) {
            Message m = iterator.next();
            if (deletingId.equals(m.getUserId())) {
                m.setUserId(null);
            }
        }
    }

    public Collection<User> getUsers() {
        return users.values();
    }

    public void setUsers(Map<Integer, User> users) {
        this.users = users;
    }

    public Element toXMLElement() {
        Element root = new Element("DataModel");
        XMLHelper xhelp = new XMLHelper();
        xhelp.addElement(root, new Attribute("name", "Users"), users);
        xhelp.addElement(root, new Attribute("name", "Messages"), messages);
        xhelp.addElement(root, new Attribute("name", "Threads"), threads);
        xhelp.addElement(root, "lastUserId", lastUserId);
        xhelp.addElement(root, "lastThreadId", lastThreadId);
        xhelp.addElement(root, "lastMessageId", lastMessageId);
        return root;
    }

    public void loadFromXMLElement(Element data) {
        Element temp = null;
        if ((temp = data.getChild("lastUserId")) != null) {
            lastUserId = Integer.valueOf(temp.getValue());
        }
        if ((temp = data.getChild("lastThreadId")) != null) {
            lastThreadId = Integer.valueOf(temp.getValue());
        }
        if ((temp = data.getChild("lastThreadId")) != null) {
            lastThreadId = Integer.valueOf(temp.getValue());
        }
        List<Element> maps = data.getChildren("Map");
        XMLHelper xhelp = new XMLHelper();
        for (Iterator iterator = maps.iterator(); iterator.hasNext();) {
            Element map = (Element) iterator.next();
            String mapName = map.getAttribute("name").getValue();
            if (mapName.equals("Users")) {
                HashMap<Integer, User> usersMap = xhelp.loadHashMap(map, "User");
                setUsers(usersMap);
            } else if(mapName.equals("Messages")) {
                HashMap<Integer, Message> msgMap = xhelp.loadHashMap(map, "Message");
                setMessages(msgMap);
            } else if(mapName.equals("Threads")) {
                HashMap<Integer, ru.ssau.entities.Thread> threadsMap = xhelp.loadHashMap(map, "Thread");
                setThreads(threadsMap);
            }
        }
    }

    //we'll do nothing here
    public void closeStore() {
        
    }

    public Collection<ru.ssau.entities.Thread> findThreadsByCaption(String caption) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Collection<Message> findMessagesByCaption(String caption) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Collection<User> findUsersByName(String name) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
